//
//  LevelMgrLayer.m
//  ProjectLila
//
//  Created by Tedo on 7/11/10.
//  Copyright 2010 sushigames. All rights reserved.
//

#import "LevelMgrLayer.h"

#import "EnemyUnit.h"
#import "GameScene.h"
#import "PlayerMenuScene.h"

@implementation LevelMgrLayer

@synthesize state;
@synthesize waveCount;
@synthesize levelInfo;
@synthesize titleLabel;

+ (id) nodeWithLevel:(NSString *)levelName {
	LevelMgrLayer *node = [super node];
	if (node != nil) {
		[node.titleLabel setString:levelName];
		
		NSString *resource = [[NSBundle mainBundle] pathForResource:@"levels" ofType:@"plist"];
		NSDictionary *levelsDictionary = [[NSDictionary alloc] initWithContentsOfFile:resource];
		node.levelInfo = [levelsDictionary valueForKey:levelName];
	}
	return node;
}

- (id) init {
	if( (self=[super init] )) {
		state = START;
		waveCount = 0;
		
		CGSize size = [[CCDirector sharedDirector] winSize];
		
		titleLabel = [CCLabel labelWithString:@"" dimensions:CGSizeMake(500, 200) alignment:UITextAlignmentCenter fontName:@"Verdana" fontSize:38];
		titleLabel.position = ccp(size.width/2, size.height/2);
		titleLabel.visible = FALSE;
		
		[self addChild:titleLabel];
		
		// update method
		[self schedule:@selector(update:)];
	}
	
	return self;
}

- (void) update:(ccTime)dt {
	if (state == START) {
		id action = [CCSequence actions:
					 [CCDelayTime actionWithDuration:0.5],
					 [CCCallFunc actionWithTarget:self selector:@selector(toggleTitleVisibility)],
					 [CCDelayTime actionWithDuration:2.0],
					 [CCCallFuncND actionWithTarget:self selector:@selector(setTitle: data:) data:(void *)@"3"],
					 [CCDelayTime actionWithDuration:1.0],
					 [CCCallFuncND actionWithTarget:self selector:@selector(setTitle: data:) data:(void *)@"2"],
					 [CCDelayTime actionWithDuration:1.0],
					 [CCCallFuncND actionWithTarget:self selector:@selector(setTitle: data:) data:(void *)@"1"],
					 [CCDelayTime actionWithDuration:1.0],
					 [CCCallFuncND actionWithTarget:self selector:@selector(setTitle: data:) data:(void *)@""],
					 [CCDelayTime actionWithDuration:1.0],
					 [CCCallFuncND actionWithTarget:self selector:@selector(setState: data:) data:(void *)WAVE],
					 nil];
		
		[self runAction:action];
	
		state = INTRO;
	}
	else if (state == INTRO) {
	}
	else if (state == BREAK) {
	}
	else if (state == WAVE) {
		NSArray *enemyUnits = [EnemyUnit allUnitsNotDead];
		if (enemyUnits.count <= 0) {
			if (waveCount == 0) 
				[self launchNextWave];
			else if (waveCount < levelInfo.count) {
				id action = [CCSequence actions:
							 [CCDelayTime actionWithDuration:2.0],
							 [CCCallFuncND actionWithTarget:self selector:@selector(setTitle: data:) data:(void *)[[NSString alloc] initWithFormat:@"Wave %d",waveCount+1]],
							 [CCDelayTime actionWithDuration:2.0],
							 [CCCallFuncND actionWithTarget:self selector:@selector(setTitle: data:) data:(void *)@""],
							 [CCCallFunc actionWithTarget:self selector:@selector(launchNextWave)],
							 [CCCallFuncND actionWithTarget:self selector:@selector(setState: data:) data:(void *)WAVE],
							 nil];
				
				[self runAction:action];
				
				state = BREAK;
			}
			else { // You Win
				id action = [CCSequence actions:
							 [CCDelayTime actionWithDuration:1.0],
							 [CCCallFuncND actionWithTarget:self selector:@selector(setTitle: data:) data:(void *)@"Victory"],
							 [CCDelayTime actionWithDuration:3.0],
							 [CCCallFunc actionWithTarget:self selector:@selector(exitLevel)],
							 nil];
				
				[self runAction:action];
				
				state = BREAK;
			}
		}
	}
}

- (void) setTitle:(id)sender data:(NSString *)text {
	[titleLabel setString:text];
}

- (void) setState:(id)sender data:(LevelState)newState {
	state = newState;
}

- (void) toggleTitleVisibility {
	titleLabel.visible = !titleLabel.visible;
}

- (void) launchNextWave {
	for (NSDictionary *unitInfo in [levelInfo objectAtIndex:waveCount]) {
		EnemyUnit *enemyUnit;
		enemyUnit = [EnemyUnit unitFromId:[unitInfo valueForKey:@"unitId"]];
		enemyUnit.position = ccp([[unitInfo valueForKey:@"startPosX"] floatValue],
								 [[unitInfo valueForKey:@"startPosY"] floatValue]);
		enemyUnit.scaleX = 0.4;
		enemyUnit.scaleY = 0.4;
		
		[[(GameScene *)[self parent] gameLayer] addChild:enemyUnit z:-enemyUnit.position.y];
	}
	
	waveCount++;
}

- (void) exitLevel {
	[[CCDirector sharedDirector] replaceScene:[CCFadeTransition transitionWithDuration:0.5f scene:[PlayerMenuScene scene]]];
}

@end
